<?php
/** @noinspection PhpUnused */

namespace app\admin\controller\shop\goods;

use app\admin\controller\shop\Base;
use ba\Tree;
use think\db\exception\DataNotFoundException;
use think\db\exception\DbException;
use think\db\exception\ModelNotFoundException;
use think\helper\Arr;
use Throwable;

/**
 * 商品分类
 */
class Classify extends Base
{
    /**
     * Classify模型对象
     * @var object
     * @phpstan-var \app\admin\model\shop\goods\Classify
     */
    protected object $model;

    protected string|array $defaultSortField = 'weigh,desc';

    protected array|string $preExcludeFields = ['id', 'create_time', 'update_time'];

    protected string|array $quickSearchField = ['name'];

    protected array $noNeedPermission = ['classify'];

    private Tree $tree;

    public function initialize(): void
    {
        parent::initialize();
        $this->model = new \app\admin\model\shop\goods\Classify;

        $this->tree = Tree::instance();
    }


    /**
     * 若需重写查看、编辑、删除等方法，请复制 @see \app\admin\library\traits\Backend 中对应的方法至此进行重写
     */

    /**
     * 列表
     * @return void
     * @throws Throwable
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     * @noinspection PhpUnusedLocalVariableInspection
     */
    public function index(): void
    {
        if ($this->request->param('select')) {
            $this->select();
        }

        list($where, $alias, $limit, $order) = $this->queryBuilder();

        $data = $this->model
            ->field($this->indexField)
            ->withJoin($this->withJoinTable, $this->withJoinType)
            ->alias($alias)
            ->where($where)
            ->order($order)
            ->select()
            ->toArray();

        //构造children
        $data = $this->tree->assembleChild($data);

        $this->success('', [
            'list'   => $data,
            'remark' => get_route_remark(),
        ]);
    }

    /**
     * 远程搜索
     * @return void
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function select(): void
    {
        $where = [];
        $param = $this->request->param();
        if (Arr::get($param, ('getParent')) == 1) {
            //获取父级分类
            $where = [
                ['pid', '=', 0]
            ];
        }
        if (Arr::get($param, 'quickSearch')) {
            $where[] = ['name', 'like', '%' . $this->request->param('quickSearch') . '%'];
        }

        $data = $this->model->where($where)->select()->toArray();

        //构造children
        $data = $this->tree->assembleChild($data);
        //构造树状
        $data = $this->tree->getTreeArray($data);
        //组装树状和children
        $data = $this->tree->assembleTree($data);

        $this->success('', [
            'options' => $data
        ]);
    }

    /**
     * 分类集
     * @noinspection PhpUnused
     * @return void
     * @throws DataNotFoundException
     * @throws DbException
     * @throws ModelNotFoundException
     */
    public function classify(): void
    {
        $result = $this->model->field(['id', 'pid', 'name as label'])->select()->toArray();
        $data   = Tree::instance()->assembleChild($result);

        foreach ($data as &$item) {
            foreach ($item['children'] as &$value) {
                $value['value'] = implode('-', [$value['pid'], $value['id']]);
            }
        }

        $this->success('', $data);
    }
}